package com.carboy.launch;

import java.util.LinkedList;
import java.util.Queue;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.carboy.launch.R;
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.SynthesizerListener;
import com.iflytek.cloud.util.ResourceUtil;
import com.iflytek.cloud.util.ResourceUtil.RESOURCE_TYPE;

public class TtsManager implements SynthesizerListener, InitListener {

	static String TAG = "TTS";
	// 语音合成对象
	private SpeechSynthesizer mTts;
	// 默认发音人
	public static String voicer = "xiaoyan";
	// 引擎类型
	private String mEngineType = SpeechConstant.TYPE_LOCAL;

	private Context mContext;
	private static TtsManager mTtsManager;
	// private int mPercentForBuffering;
	// private Object mPercentForPlaying;

	// 当前播放的信息文字
	private String mCurPlayText;
	boolean isfinish = true;
	private boolean silence = false;
	private static Queue<String> queue;

	TtsManager(Context context) {
		mContext = context;
		SpeechUtility.createUtility(mContext,
				"appid=" + mContext.getString(R.string.app_id));
		// 初始化合成对象
		mTts = SpeechSynthesizer.createSynthesizer(mContext, this);
		queue = new LinkedList<String>();
	}

	public static TtsManager getInstance(Context context) {
		if (mTtsManager == null) {
			mTtsManager = new TtsManager(context);
		}
		if (queue == null) {
			queue = new LinkedList<String>();
		}
		return mTtsManager;
	}

	/***
	 * 
	 * @author James
	 * @Description 带队列的语音播报
	 * @param text
	 */

	public void playText(String text) {
		if (!queue.contains(text)) {
			queue.offer(text);
		}
		Log.i(TAG, ">> " + text);
		Play();
	}

	/**
	 * 
	 * @author James
	 * @Description 播报
	 */
	public void Play() {

		if (!mTts.isSpeaking()) {
			String text = queue.poll();
			if (null != text) {
				if (silence) {
					return;
				}
				// 设置参数
				setParam();
				int code = mTts.startSpeaking(text, this);
				if (code != ErrorCode.SUCCESS) {
					Log.d(TAG, "语音合成失败,错误码: " + code);
				}
			}
		}

	}

	/***
	 * 
	 * @author James
	 * @Description 不带队列的语音播报
	 * @param text
	 */

	public void playText2(String text) {
		Log.i(TAG, ">> " + text);
		String ttsText = "。 " + text;

		Play2(ttsText);
	}

	public void Play2(String text) {

		if (silence) {
			return;
		}
		// 设置参数
		setParam();

		int code = mTts.startSpeaking(text, this);
		if (code != ErrorCode.SUCCESS) {
			Log.d(TAG, "语音合成失败,错误码: " + code);
		}
	}

	/**
	 * 参数设置
	 * 
	 * @param param
	 * @return
	 */
	private void setParam() {
		// 清空参数
		mTts.setParameter(SpeechConstant.PARAMS, null);
		if (mEngineType.equals(SpeechConstant.TYPE_LOCAL)) {
			// 设置发音人资源路径
			// mTts.setParameter(ResourceUtil.TTS_RES_PATH,getResourcePath());
			// Log.e(TAG, getResourcePath());
		}

		// 设置使用引擎
		mTts.setParameter(SpeechConstant.ENGINE_TYPE, mEngineType);
		// 设置发音人
		mTts.setParameter(SpeechConstant.VOICE_NAME, voicer);

		/*
		 * //设置语速
		 * mTts.setParameter(SpeechConstant.SPEED,mSharedPreferences.getString
		 * ("speed_preference", "50"));
		 * 
		 * //设置音调
		 * mTts.setParameter(SpeechConstant.PITCH,mSharedPreferences.getString
		 * ("pitch_preference", "50"));
		 * 
		 * //设置音量
		 * mTts.setParameter(SpeechConstant.VOLUME,mSharedPreferences.getString
		 * ("volume_preference", "50"));
		 * 
		 * //设置播放器音频流类型
		 * mTts.setParameter(SpeechConstant.STREAM_TYPE,mSharedPreferences
		 * .getString("stream_preference", "3"));
		 */
	}

	// 获取发音人资源路径
	private String getResourcePath() {
		StringBuffer tempBuffer = new StringBuffer();
		// 合成通用资源
		tempBuffer.append(ResourceUtil.generateResourcePath(mContext,
				RESOURCE_TYPE.assets, "tts/common.jet"));
		tempBuffer.append(";");
		// 发音人资源
		tempBuffer.append(ResourceUtil.generateResourcePath(mContext,
				RESOURCE_TYPE.assets, "tts/" + voicer + ".jet"));
		return tempBuffer.toString();
	}

	public void onDestroy() {
		mTts.stopSpeaking();
		// 退出时释放连接
		mTts.destroy();
	}

	@Override
	public void onBufferProgress(int percent, int beginPos, int endPos,
			String info) {
		// TODO Auto-generated method stub
		/*
		 * mPercentForBuffering = percent; String str =
		 * String.format(getString(R.string.tts_toast_format),
		 * mPercentForBuffering, mPercentForPlaying);
		 * 
		 * Log.d(TAG, str);
		 */
	}

	@Override
	public void onCompleted(SpeechError error) {
		// TODO Auto-generated method stub
		if (error == null) {
			Log.d(TAG, "播放完成");
		} else {
			Log.d(TAG, error.getPlainDescription(true));
		}
		// Play();
	}

	@Override
	public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSpeakBegin() {
		// TODO Auto-generated method stub
		isfinish = false;
		Log.d(TAG, "开始播放");
	}

	@Override
	public void onSpeakPaused() {
		// TODO Auto-generated method stub
		Log.d(TAG, "暂停播放");
	}

	@Override
	public void onSpeakProgress(int percent, int beginPos, int endPos) {
		// TODO Auto-generated method stub
		/*
		 * mPercentForPlaying = percent; String str =
		 * String.format(getString(R.string.tts_toast_format),
		 * mPercentForBuffering, mPercentForPlaying); Log.d(TAG, str);
		 */
	}

	@Override
	public void onSpeakResumed() {
		// TODO Auto-generated method stub
		Log.d(TAG, "继续播放");
	}

	@Override
	public void onInit(int arg0) {
		// TODO Auto-generated method stub
		if (arg0 == ErrorCode.SUCCESS) {
			// Log.d(TAG, "初始化失败,错误码："+arg0);

		}
	}

	public boolean getSilence() {
		return silence;
	}

	public void setSilence(boolean silence) {
		this.silence = silence;
	}
}
